package cn.edu.njpi.web;

import cn.edu.njpi.model.TCollage;
import cn.edu.njpi.model.TSchool;
import cn.edu.njpi.model.TSchoolAdmin;
import cn.edu.njpi.model.TUser;
import cn.edu.njpi.service.TCollageService;
import cn.edu.njpi.service.TSchoolAdminService;
import cn.edu.njpi.service.TSchoolService;
import cn.edu.njpi.utils.InformUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.web
 * @date 2022/4/20 4:41 PM
 */
@RequestMapping(value = "/collage")
@Controller
public class CollageWeb {
    @Autowired
    private TCollageService tCollageService;
    @Autowired
    private TSchoolAdminService tSchoolAdminService;
    @Autowired
    private TSchoolService tSchoolService;
    @Value("${caoxingfen.url}")
    private String url;

    @RequestMapping(value = "/admin")
    public ModelAndView collageAdmin(String token) {
        ModelAndView modelAndView = new ModelAndView("/collage/admin");
        modelAndView.addObject("url", url);
        modelAndView.addObject("title", InformUtils.title);
        TUser tUser = (TUser) InformUtils.map.get(token);
        if (tUser.getUserPermssionId() == 1) {
            List<TCollage> tCollageList = tCollageService.getAllCollage();
            List<TSchool> allSchool = tSchoolService.getAllSchool();
            modelAndView.addObject("tCollageList", tCollageList);
            modelAndView.addObject("tSchoolList", allSchool);
        } else if (tUser.getUserPermssionId() == 2) {
            TSchoolAdmin tSchoolAdmin = tSchoolAdminService.selectTSchoolAdminByUserId(tUser.getUserId());
            TSchool s = tSchoolService.selectTSchoolBySchoolId(tSchoolAdmin.getSchoolId());
            List<TSchool> tSchoolList = new ArrayList<>();
            tSchoolList.add(s);
            List<TCollage> tCollageList = tCollageService.selectCollageBySchoolId(tSchoolAdmin.getSchoolId());
            modelAndView.addObject("tCollageList", tCollageList);
            modelAndView.addObject("tSchoolList", tSchoolList);
        }
        return modelAndView;
    }
}
